package employee;

import utilities.JDBC.Database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Objects;

/**
 * 账号信息类
 */
public class User implements Database.ToObject<User> {
    private int id;
    private String name;
    private String password;

    public User() {
    }

    public User(int id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }

    /**
     * 函数接口 向外部暴露对象构造逻辑
     *
     * @param set 得到的结果集
     * @return 返回一个构造完成的对象
     * @throws SQLException 发生的结果集错误
     */
    @Override
    public User getObject(ResultSet set) throws SQLException {
        return new User(set.getInt("userId"),set.getString("userName"),set.getString("userPassword"));
    }

    /**
     * 获取账号编号
     * @return id 编号
     */
    public int getId() {
        return id;
    }

    /**
     * 设置账号编号
     * @param id 编号
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * 获取账号名字
     * @return name 名字
     */
    public String getName() {
        return name;
    }

    /**
     * 设置账号名字
     * @param name 名字
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取账号密码
     * @return password 密码
     */
    public String getPassword() {
        return password;
    }

    /**
     * 设置账号密码
     * @param password 密码
     */
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User user)) return false;
        return getId() == user.getId() && Objects.equals(getName(), user.getName()) && Objects.equals(getPassword(), user.getPassword());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId(), getName(), getPassword());
    }

    public String toString() {
        return "User{id = " + id + ", name = " + name + ", password = " + password + "}";
    }
}
